package com.aash.persistence.dao;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.aash.persistence.exception.DaoException;
import com.aash.persistence.exception.DaoExceptionEnum;
import com.aash.persistence.generic.GenericDaoJpaImpl;
import com.aash.persistence.model.User;

@Component
public class UserDaoImpl extends GenericDaoJpaImpl<User, Long> implements
		UserDao {

	public UserDaoImpl() {
		super(User.class);
	}

	@Override
	@SuppressWarnings("unchecked")
	public User getUserByEmail(String email) throws DaoException {
		Query query = getEm().createQuery("select from User where email = ?1");
		query.setParameter(1, email);
		List<User> result = (List<User>) query.getResultList();
		if(result == null || result.size() < 1) {
			throw new DaoException(DaoExceptionEnum.NO_RESULT, email);
		}
		return result.get(0);
	}
}
